# Filters added to this controller apply to all controllers in the application.
# Likewise, all the methods added will be available for all controllers.

class ApplicationController < ActionController::Base
  helper :all # include all helpers, all the time

  # See ActionController::RequestForgeryProtection for details
  # Uncomment the :secret if you're not using the cookie session store
  protect_from_forgery # :secret => 'ebac380fa0eda87113f01e5d04236c96'
  
  helper_method :init_custom_includes
  helper_method :init_trace_nav
  helper_method :current_user
  helper_method :public_dir
  helper_method :authenticate
  helper_method :thumbnail_url
  helper_method :deny_permission

  private
  def init_custom_includes
    @custom_stylesheets = Array.new
    @custom_scripts = Array.new
  end
  
  def init_trace_nav
    @trace_nav = Array.new
  end

  def current_user_session
    return @current_user_session if defined?(@current_user_session)
    @current_user_session = UserSession.find
  end

  def current_user
    return @current_user if defined?(@current_user)
    @current_user = current_user_session && current_user_session.record
  end

  def thumbnail_url(photo, request)
    if photo != nil
      return "http://" << request.domain << ":" << request.port.to_s << photo.thumbnail_relative_url
    else
      return ""
    end
  end

  def deny_permission
    flash[:notice] = "You don't have permission for that! Please login"
    session[:return_to] = request.request_uri
    respond_to do |format|
      format.html { redirect_to '/login' }
      format.xml { head :ok }
    end
  end

  def authenticate
    if @entry != nil
      if current_user && @entry.content_generator == current_user
        return true
      else
        deny_permission
      end
    else
      if current_user
        return true
      end
    end
    return false
  end
end
